package com.guiwu.gteway.industryapp.Device.View;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.guiwu.gteway.industryapp.Device.module.GateWay;
import com.guiwu.gteway.industryapp.GateWayInfo.GateWayInfoContract;
import com.guiwu.gteway.industryapp.R;
import com.guiwu.gteway.industryapp.util.MyLog;

/**
 * Created by qiaoguan on 2018/3/8. 监控点基本信息
 */

public class MonitorInfoFragment extends Fragment implements GateWayInfoContract.View {
    private static final String TAG = "MonitorInfoFragment";
    private ScrollView scrollView;
    private LinearLayout ly;
    private MonitorActivity activityParent;
    private GateWay fragGateWay;

    private TextView nickName;
    private LinearLayout nickNameLy;
    private TextView shareName;
    private LinearLayout connectLayout;
    private LinearLayout sysSettingLayout;
    private TextView deviceSerial;
    private TextView netMode;
    private TextView deviceState;
    private TextView groups;
    private LinearLayout remarkLayout;

    private float Xd,Yd;

    private GateWayInfoContract.Presenter mPresent;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityParent = (MonitorActivity) getActivity();
        fragGateWay = getArguments().getParcelable(MonitorActivity.GATEWAY);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.monitor_info_layout,container,false);
        initView(v);
        updateUi(fragGateWay);
        return v;
    }


    private void initView(View v){
        scrollView = v.findViewById(R.id.monitor_info_scroll);
        nickName = v.findViewById(R.id.ds_nick_name);
        shareName = v.findViewById(R.id.ds_share_name);
        deviceSerial = v.findViewById(R.id.ds_device_serial);
        deviceState = v.findViewById(R.id.ds_state);
        groups = v.findViewById(R.id.ds_groups);
        ly = v.findViewById(R.id.monitor_info_linearLayout);
        remarkLayout = v.findViewById(R.id.ds_remarks);
        nickNameLy = v.findViewById(R.id.ds_nick_name_layout);
        sysSettingLayout = v.findViewById(R.id.ds_sys_setting);

        MonitorActivity.stopParentScroll(scrollView);
        MonitorActivity.stopParentScroll(ly);
        MonitorActivity.stopParentScroll(remarkLayout);
        MonitorActivity.stopParentScroll(sysSettingLayout);
        MonitorActivity.stopParentScroll(nickNameLy);

    }

    private void updateUi(GateWay gateWay){
        fragGateWay = gateWay;
        nickName.setText(gateWay.getGateWayName());
        deviceSerial.setText(gateWay.getGateWayId());
        groups.setText(gateWay.getFromGroup());
    }


    public static MonitorInfoFragment newInstance(GateWay mGateWay) {
        Bundle args = new Bundle();
        args.putParcelable(MonitorActivity.GATEWAY,mGateWay);
        MonitorInfoFragment fragment = new MonitorInfoFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onResume() {
        super.onResume();
        mPresent.askForShareName(fragGateWay.getSn());
        mPresent.askForConnectType(fragGateWay.getSn());
    }

    @Override
    public void onGetShareName(boolean result, GateWay gateWay, String msg) {
        if (result){
            fragGateWay = gateWay;
            //MyLog.i(TAG,fragGateWay.getShareName() + "");
            shareName.setText(fragGateWay.getShareName());
        }
    }

    @Override
    public void onConnectType(boolean result, GateWay gateWay,String msg) {
        if (result){
            fragGateWay = gateWay;
           // MyLog.i(TAG,fragGateWay.getConnectType() + "");
            shareName.setText(fragGateWay.getConnectType());
        }
    }

    @Override
    public void setPresenter(GateWayInfoContract.Presenter presenter) {
        this.mPresent = presenter;
    }
}
